﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STS.Schema.Interfaces;
using STS.Schema.Collections;
using STS.Schema.Base;
using STS.Schema.Enums;
using STS.Interfaces;

namespace STS.Schema.MSSql
{
    public class MSSqlDatabase : SqlObjectBase, IDatabase
    {
        private Lazy<ISqlObjectCollection> _tableList;
        private Lazy<ISqlObjectCollection> _viewList;
        private Lazy<ISqlObjectCollection> _spList;
        private Lazy<ISqlObjectCollection> _tableFunctionList;
        private Lazy<ISqlObjectCollection> _scalarFunctionList;

        public override SqlObjectType ObjectType
        {
            get { return SqlObjectType.Database; }
        }

        [SqlQueryProperty("name")]
        public override string Name
        {
            get
            {
                return base.Name;
            }
            internal set
            {
                base.Name = value;
            }
        }

        [SqlQueryProperty("dbid")]
        public override int ObjectID
        {
            get
            {
                return base.ObjectID;
            }
            internal set
            {
                base.ObjectID = value;
            }
        }

        public ISqlObjectCollection TableList { get { return _tableList.Value; } }

        public ISqlObjectCollection ViewList { get { return _viewList.Value; } }

        public ISqlObjectCollection StoredProcedureList { get { return _spList.Value; } }

        public ISqlObjectCollection TableFunctionList { get { return _tableFunctionList.Value; } }

        public ISqlObjectCollection ScalarFunctionList { get { return _scalarFunctionList.Value; } }


        public MSSqlDatabase()
            : this(null, null)
        { }

        public MSSqlDatabase(ISchemaFactory schemaFactory, IDatabase database)
            : base(schemaFactory, null)
        {
            InitLazyObjects();
        }

        private void InitLazyObjects()
        {
            _tableList = new Lazy<ISqlObjectCollection>(() =>
            {
                return GetChildren<MSSqlTable>();
            });
            _viewList = new Lazy<ISqlObjectCollection>(() =>
            {
                return GetChildren<MSSqlView>();
            });
            _spList = new Lazy<ISqlObjectCollection>(() =>
            {
                return GetChildren<MSSqlStoredProcedure>();
            });
            _tableFunctionList = new Lazy<ISqlObjectCollection>(() =>
            {
                return GetChildren<MSSqlTableValuedFunction>();
            });
            _scalarFunctionList = new Lazy<ISqlObjectCollection>(() =>
            {
                return GetChildren<MSSqlScalarFunction>();
            });
        }

        public string Owner { get; internal set; }

        public string UserName { get; internal set; }
        
        public override ISqlObjectCollection GetChildren<TChild>()
        {
            return base.Factory.GetObjectList<TChild>(this, null, this);
        }

        public override void Refresh()
        {
            base.Refresh();
            base.Factory.ClearObjectList<MSSqlTable>(base.Database);
            base.Factory.ClearObjectList<MSSqlView>(base.Database);
            base.Factory.ClearObjectList<MSSqlStoredProcedure>(base.Database);
            base.Factory.ClearObjectList<MSSqlTableValuedFunction>(base.Database);
            base.Factory.ClearObjectList<MSSqlScalarFunction>(base.Database);
            InitLazyObjects();
        }
    }
}
